package com.cckat.controller;

import com.cckat.base.BaseController;
import com.cckat.entity.Admin;
import com.cckat.entity.Role;
import com.cckat.service.AdminRoleService;
import com.cckat.service.AdminService;
import com.cckat.service.RoleService;
import com.cckat.utils.FileUtil;
import com.cckat.utils.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author 静海基地
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @DubboReference
    private AdminService adminService;

    @DubboReference
    private RoleService roleService;

    @DubboReference
    private AdminRoleService adminRoleService;

    @RequestMapping
    public String findPage(Model model, @RequestParam Map<String, Object> filters) {
        //调用service处理业务逻辑
        PageInfo<Admin> page = adminService.findPage(filters);
        //将filters和page共享到请求域
        model.addAttribute("filters", filters);
        model.addAttribute("page", page);
        return "admin/index";
    }


    //打开新增页面（窗口）
    @RequestMapping("/create")
    public String create() {
        return "admin/create";
    }

    /**
     * 在新增界面保存已输入的用户数据
     *
     * @param admin
     * @param model
     * @return
     */
    @RequestMapping("/save")
    public String insert(Model model, Admin admin) {
        //对密码加密
        admin.setPassword(new BCryptPasswordEncoder().encode(admin.getPassword()));
        //调用service处理业务逻辑
        adminService.insert(admin);
        //向请求域共享提示信息并跳转到common/successPage.html
        return successPage("新增用户成功", model);
    }

    @RequestMapping("/edit/{id}")
    public String getById(@PathVariable("id") Long id, Model model) {
        Admin admin = adminService.getById(id);
        model.addAttribute("admin", admin);
        return "admin/edit";
    }

    @RequestMapping("/update")
    public String update(Admin admin, Model model) {
        adminService.update(admin);
        return successPage("修改用户成功", model);

    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable("id") Long id, Model model) {
        model.addAttribute("id", id);
        return "admin/upload";
    }


    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable("id") Long id, MultipartFile file, Model model) throws IOException {
        //使用uuid作为文件名
        String fileName = FileUtil.getUUIDName(file.getOriginalFilename());
        //将文件上传到七牛云
        QiniuUtil.upload2Qiniu(file.getBytes(), fileName);
        //更新用户头像的路径
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://riyf343zt.hn-bkt.clouddn.com/"+fileName);
        adminService.update(admin);
        return successPage("用户头像上传成功",model);
    }

    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, @RequestParam("roleIds") List<Long> roleIds, Model model){
        adminRoleService.assignRole(adminId, roleIds);
        return successPage("分配角色成功", model );
    }

    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId, Model model){
        model.addAttribute("adminId", adminId);
        Map<String, List<Role>> map = roleService.findRoleList(adminId);
        model.addAllAttributes(map);
        return "admin/assignShow";
    }
    //test_gitee
    //test_gitee2
    //test_gitee3
}